package gtgold;

import java.io.Serializable;
import java.util.EnumMap;
import java.util.Map;

/***
 * Associates a double value with each Item and behaves like a vector
 * 
 * @author R
 */
public class ItemVec implements Serializable
{
	private static final long serialVersionUID = 1L;
	private static ItemVec weights;
	private final Map<Item, Double> data;

	/***
	 * Construct an empty vector, initialized to 0
	 */
	public ItemVec()
	{
		data = new EnumMap<Item, Double>(Item.class);
		for (Item i : Item.values())
		{
			set(i, 0);
		}
	}

	/**
	 * Retrieve amount of an item
	 * 
	 * @param key of item to retrieve
	 * @return amount of that item
	 */
	public double get(Item key)
	{
		return data.get(key);
	}

	public ItemVec set(Item key, double val)
	{
		data.put(key, val);
		return this;
	}
	
	//Convenience function to add an amount to a single item
	public void add(Item key, double val)
	{
		set(key, get(key) + val);
	}	

	public void assign(ItemVec other)
	{
		for (Item i : Item.values())
		{
			set(i, other.get(i));
		}
	}

	public void addeq(ItemVec other)
	{
		for (Item i : Item.values())
		{
			set(i, get(i) + other.get(i));
		}
	}

	/*
	 * public ItemVec add(ItemVec other){ ItemVec temp = new ItemVec();
	 * temp.addeq(this); temp.addeq(other); return temp; }
	 */

	public void subeq(ItemVec other)
	{
		for (Item i : Item.values())
		{
			set(i, get(i) - other.get(i));
		}
	}

	/*
	 * public ItemVec sub(ItemVec other){ ItemVec temp = new ItemVec();
	 * temp.subeq(this); temp.subeq(other); return temp; }
	 */
	public double dot(ItemVec other)
	{
		double sum = 0;
		for (Item i : Item.values())
		{
			sum += get(i) * other.get(i);
		}

		return sum;
	}

	// Special methods
	public double weight()
	{
		if (weights == null)
		{
			// weights = new ItemVec().set(Item.FOOD, 1).set(Item.CLOTHES,
			// 2).set(Item.BULLETS, 3).
			// set(Item.AXLES, 125).set(Item.TOUNGES, 100).set(Item.WHEELS, 75);
			weights = new ItemVec();

			for (Item i : Item.values())
			{
				weights.set(i, i.getWeight());
			}
		}

		return this.dot(weights);
	}
}
